import G from "../Utilities/Global";
import MapData from "../Map/MapData";

/**
 * 所有地图信息
 * created by Five on 2018-11-5 14:33:08
 */

export default class MapDatasConfig {
    // 已缓存的地图信息
    public Maps: Map<string, MapData>
    public MapNamesArr: Array<string>;

    constructor() {
        this.Maps = new Map<string, MapData>();
        this.MapNamesArr = new Array<string>();
        this.init();
    }

    private async init() {
        let assets = await G().Res.LoadDir("config/maps") as Array<any>;
        assets.forEach(asset => {
            let ms = asset.json as MapData;
            this.Maps.set(ms.MapName ? ms.MapName : asset.name, ms);
            this.MapNamesArr.push(ms.MapName ? ms.MapName : asset.name);
        })
        cc.log(this.Maps)
    }

    public GetMap(name: string): MapData {
        let result = this.Maps.get(name);
        if (result == null)
            result = new MapData();
        return result;
    }

    public GetNext(cur:number):number{
        if(cur >= this.MapNamesArr.length - 1)
            return 0;
        return cur+1;
    }

    public GetMapByIndex(index: number): MapData {
        let values = Array.from(this.Maps.values());
        return values[index];
    }

    public GetIndexByName(name: string) {
        return this.MapNamesArr.indexOf(name);
    }

    public GetNameByIndex(index: number): string {
        if (index < 0 || index > this.MapNamesArr.length)
            return "";
        let result = this.MapNamesArr[index];
        if (result == null)
            result = "";
        return result;
    }
}
